<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8" %>
<%@ page import="java.util.List" %>
<%@ page import="java.text.SimpleDateFormat" %>
<%@ page import="java.sql.SQLException" %>
<%@ page import="java.time.format.DateTimeFormatter" %>
<%@ page import="com.demo.main.dao.MessageDao" %>
<%@ page import="com.demo.main.entity.Message" %>
<%@ page import="com.demo.main.utils.CommonUtil" %>
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
<%
    try {
        Class.forName("com.mysql.jdbc.Driver");
        MessageDao messageDao = new MessageDao();
        List<Message> messages = messageDao.getAllMessages();

    DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss");
%>
<html lang="zh-CN">
<head>
    <title>留言板 - 查看留言</title>
    <link rel="stylesheet" href="/resources/css/index.css">
    <link rel="stylesheet" href="/resources/css/boots.css">
    <link rel="stylesheet" href="/resources/css/boot.css">
    <style>
        .non-top-nav {
            display: flex;
        }
        .container-fluid h1 {
            margin-bottom: 20px;
        }

        table {
            border-collapse: collapse;
            width: 100%;
        }

        th {
            background-color: #333;
            color: #fff;
            padding: 10px;
        }

        td {
            text-align: center;
            padding: 10px;
            border-bottom: 1px solid #ccc;
        }

        tr:last-child td {
            border-bottom: none;
        }

    </style>
</head>
<body>
<%
    // public
    String username = (String) request.getSession().getAttribute("username");
    String identity = CommonUtil.getIdentity();
    List<String[]> permissions = (List<String[]>) request.getAttribute("permissions");
%>
<div class="container-fluid">
    <div class="top-nav">
        <div class="top-nav-container">
            <div class="left-box">
                <p>图书管理系统</p>
            </div>
            <div class="right-box">
                <p class="top-nav-username" style="margin-right: 10px">用户：<%=username%></p>
                <p class="top-nav-identity">类型：<%=identity.equals("user") ? "用户" : "管理员"%></p>
                <a href="/login?method=logout">
                    <button style="margin-left: 10px">退出登录</button>
                </a>
            </div>
        </div>
    </div>
    <div class="non-top-nav">
        <div class="sidebar">
            <ul class="sidebar-ul">
                <c:forEach var="item" items="<%=permissions%>">
                    <li><a href="${item[1]}">${item[0]}</a></li>
                </c:forEach>
            </ul>
        </div>
        <div class="main" style="margin-left: 0">
            <h1>留言板 - 查看留言</h1>
            <table>
                <tr>
                    <th>用户名</th>
                    <th>留言内容</th>
                    <th>时间</th>
                </tr>
                <% for (Message message : messages) { %>
                <tr>
                    <td><%= message.getUsername() %></td>
                    <td><%= message.getContent() %></td>
                    <td><%= message.getTimestamp().format(formatter) %></td>
                </tr>
                <% } %>
            </table>
        </div>
    </div>
</div>
</body>
</html>
<%
    } catch (SQLException e) {
        out.println("Error: " + e.getMessage());
    }
%>
